# - Try to find libdwarf library and headers
# Once done, this will define
#
#  LIB_DWARF_FOUND - system has libdwarf
#  LIB_DWARF_INCLUDE_DIR - the libdwarf include directories
#  LIB_DWARF_LIBRARIES - link these to use libdwarf

if (LIB_DWARF_INCLUDE_DIR AND LIB_DWARF_LIBRARIES)
	set(LIB_DWARF_FIND_QUIETLY TRUE)
endif ()

if (LIB_DWARF_INSTALL_PATH)
	find_path(LIB_DWARF_INCLUDE_DIR
			NAMES libdwarf/dwarf.h libdwarf/libdwarf.h
			PATHS ${LIB_DWARF_INSTALL_PATH}/include
			NO_DEFAULT_PATH)
	if (NOT CYGWIN)
		find_library(LIB_DWARF_LIBRARIE_DWARF
				NAMES dwarf
				PATHS ${LIB_DWARF_INSTALL_PATH}/lib
				NO_DEFAULT_PATH)
		if (NOT ${LIB_DWARF_LIBRARIE_DWARF} STREQUAL "NOTFOUND")
			set(LIB_DWARF_LIBRARIES ${LIB_DWARF_LIBRARIE_DWARF})
		endif ()
	endif ()
else ()
	find_path(LIB_DWARF_INCLUDE_DIR
			NAMES libdwarf/dwarf.h libdwarf/libdwarf.h)
	if (NOT CYGWIN)
		find_library(LIB_DWARF_LIBRARIE_DWARF
				NAMES dwarf)
		if (NOT ${LIB_DWARF_LIBRARIE_DWARF} STREQUAL "NOTFOUND")
			set(LIB_DWARF_LIBRARIES ${LIB_DWARF_LIBRARIE_DWARF})
		endif ()
	endif ()
endif ()

include(FindPackageHandleStandardArgs)
if (NOT CYGWIN)
	find_package_handle_standard_args(LIB_DWARF DEFAULT_MSG LIB_DWARF_INCLUDE_DIR LIB_DWARF_LIBRARIES)
else ()
	find_package_handle_standard_args(LIB_DWARF DEFAULT_MSG LIB_DWARF_INCLUDE_DIR)
endif ()

if (NOT LIB_DWARF_FOUND)
	MESSAGE(SEND_ERROR "libdwarf install path is invalid or cannot to be find, please install it or specify it by \"-DLIB_DWARF_INSTALL_PATH=<value>\".")
endif ()

if (NOT CYGWIN)
	mark_as_advanced(LIB_DWARF_INCLUDE_DIR LIB_DWARF_LIBRARIES)
else ()
	mark_as_advanced(LIB_DWARF_INCLUDE_DIR)
endif ()
